package com.xpec.c4.message.login;

import java.io.Serializable;
import java.util.Calendar;
import java.util.concurrent.TimeUnit;

public class SAccountLoginTokenInfo implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = -1755924751749892661L;
	/**
	 * 隨機產生值
	 */
	private String randomToken;
	/**
	 * 是否透過f5刷新登入
	 */
	private boolean isReconnect;
	
	/**
	 * 過期時間長度 (ms)
	 */
	private long expirePeriod;
	
	/**
	 * 執行結果
	 */
	private int errorCode;
	
	/**
	 * server 所在時區的時間偏移量 (ms)
	 */
	private int timeZoneOffset;

	public SAccountLoginTokenInfo(){
		initial();
	}
	public SAccountLoginTokenInfo(String randomToken , boolean isReconnect, long expirePeriod){
		initial();
		this.randomToken = randomToken;
		this.isReconnect = isReconnect;
		this.expirePeriod = expirePeriod;
	}
	private void initial() {
		Calendar cal = Calendar.getInstance();
		timeZoneOffset = cal.get(Calendar.ZONE_OFFSET);
	}
	public String getRandomToken() {
		return randomToken;
	}
	public void setRandomToken(String randomToken) {
		this.randomToken = randomToken;
	}
	public boolean isReconnect() {
		return isReconnect;
	}
	public void setReconnect(boolean isReconnect) {
		this.isReconnect = isReconnect;
	}
	public long getExpirePeriod() {
		return expirePeriod;
	}
	public void setExpirePeriod(long expirePeriod) {
		this.expirePeriod = expirePeriod;
	}
	public int getErrorCode() {
		return errorCode;
	}
	public void setErrorCode(int errorCode) {
		this.errorCode = errorCode;
	}
	public int getTimeZoneOffset() {
		return timeZoneOffset;
	}
	public void setTimeZoneOffset(int timeZoneOffset) {
		this.timeZoneOffset = timeZoneOffset;
	}
	public String getTimeZoneOffsetString() {
		long offsetHour = TimeUnit.MILLISECONDS.toHours(timeZoneOffset);
		long offsetMinute = TimeUnit.MILLISECONDS.toMinutes(timeZoneOffset) - TimeUnit.HOURS.toMinutes(offsetHour);
		offsetMinute = Math.abs(offsetMinute);
		
		String res = null;
		if (offsetHour > 0) {
			res = String.format("GMT+%d:%02d", offsetHour, offsetMinute);
		} else {
			res = String.format("GMT%d:%02d", offsetHour, offsetMinute);
		}
		
		return res;
	}
}
